// 游戏正式开始
let customsetin;
function StartGame() {
    customsetin = setInterval(() => {
        // 添加顾客队列
        Custom()
        // 后续应该在这里添加耐心值?
    }, 10000)
    Startcook()
}

function Startcook() {
    // 厨师开始做饭啦
    setInterval(() => {
        // 判断食物队列是否有食物，有 => 给厨师做饭
        // 厨师做饭的时间 : 冷食4秒，主菜20秒，饮品10秒
        if (allfood.length !== 0) {
            for (let i = 0; i < allcooker.length; i++) {
                // 循环总厨师数量的次数
                if (allcooker[i].free === 0) {
                    allcooker[i].free = 1
                    allcooker[i].food = allfood.shift()
                    let t = allcooker[i].food.food_name,
                        user_name = allcooker[i].food.user_name,  // 点菜的顾客姓名
                        money = allcooker[i].food.money
                    // console.log(cook.children[i])
                    // console.log(allcooker[i])
                    cook.children[i].style.backgroundColor = 'blue'
                    cook.children[i].children[0].style.display = 'block'
                    cook.children[i].children[0].innerHTML = t
                    if (t === '凉拌SAN' || t === '冷切DOM') {
                        setTimeout(() => {
                            // 设置一个时间 => 做饭完成
                            // 做饭完成之后需要将顾客||顾客菜品移除
                            showfood(i)  // 展示端菜图片
                            setTimeout(() => {
                                allcooker[i].food = ''
                                allcooker[i].free = 0
                                cook.children[i].style.backgroundColor = '#ccc'
                                cook.children[i].children[0].style.display = 'none'
                                let m = document.getElementsByClassName('money')[0].children[0]
                                m.innerHTML = parseInt(m.innerHTML) + parseInt(money)
                                Startseat(user_name)
                            }, 1500)
                        }, 4000)
                    } else if (t === 'UL炖LI' || t === '红烧HEAD' || t === '酥炸ECharts' || t === '炙烤CSS' || t === '清蒸DIV') {
                        setTimeout(() => {
                            // 设置一个时间 => 做饭完成
                            showfood(i)  // 展示端菜图片
                            setTimeout(() => {
                                allcooker[i].food = ''
                                allcooker[i].free = 0
                                cook.children[i].style.backgroundColor = '#ccc'
                                cook.children[i].children[0].style.display = 'none'
                                let m = document.getElementsByClassName('money')[0].children[0]
                                m.innerHTML = parseInt(m.innerHTML) + parseInt(money)
                                Startseat(user_name)
                            }, 1500)
                        }, 20000)
                    } else {
                        setTimeout(() => {
                            // 设置一个时间 => 做饭完成
                            showfood(i)  // 展示端菜图片
                            setTimeout(() => {
                                allcooker[i].food = ''
                                allcooker[i].free = 0
                                cook.children[i].style.backgroundColor = '#ccc'
                                cook.children[i].children[0].style.display = 'none'
                                let m = document.getElementsByClassName('money')[0].children[0]
                                m.innerHTML = parseInt(m.innerHTML) + parseInt(money)
                                Startseat(user_name)
                            }, 1500)
                        }, 10000)
                    }
                }
            }
        }
    }, 3000);
}
// 座位的标号为0--3四个座位，其中有一个userinfo属性，内含有.name,.food,.touxiang,.waittime,.money
//document.getElementsByTagName('body')[0].children[15].removeChild(show_info_custom[0].children[0])   移除顾客框内菜品

function Startseat(name) {
    // 更新座位信息 ———— 传入顾客名字，寻找名字对应的座位号0——3
    let right = [1, 2, 3, 4]
    for (let i = 0; i < custom.children.length; i++) {
        if (custom.children[i].style.backgroundImage !== '') {
            if (custom.children[i].userinfo.name === name) {
                custom.children[right[i-1]].children[0].removeChild(show_info_custom[i-1].children[0])
                if (custom.children[right[i-1]].children[0].children.length === 0) {
                    S_Tips(`顾客${name}已用餐完毕`)
                    showmoney(i)
                    setTimeout(() => {                        
                        custom.children[i].style.backgroundColor = '#ccc'
                        custom.children[i].style.backgroundImage = ''
                    }, 1500)

                }
            }
        }

    }
}

function Day_day() {
    var week = document.getElementsByClassName('day')[0].children[0],
        day = document.getElementsByClassName('day')[0].children[1]

    setInterval(() => {
        var s = [...day.innerHTML]
        s.shift()
        s = parseInt(s.join('')) + 1
        day.innerHTML = `D${s}`

        if (s % 8 === 0) {
            var w = [...week.innerHTML]
            w.shift()
            w = parseInt(w.toString()) + 1
            week.innerHTML = `W${w}`
            day.innerHTML = `D1`
            // 若一周结束，则需要支付相应工资
            // 操作金额 ———— 一个厨师120元
            let m = document.getElementsByClassName('money')[0].children[0]
            m.innerHTML = parseInt(m.innerHTML) - 120 * allcooker.length
            /*************************** */
            // 提示用户
            S_Tips(`本周支付厨师费用${120 * allcooker.length}元`)
        }
    }, 120000) // 120秒一天
}
